package com.mjk.common.web.getway.restful.prototal.websocket.impl.instance;


import cn.hutool.core.exceptions.ExceptionUtil;
import com.mjk.common.base.error.BeeError;
import com.mjk.common.base.response.BeeResponse;
import com.mjk.common.tools.gson.GsonUtils;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.tools.springextend.BeeSpringBean;
import com.mjk.common.tools.xml.XmlUtils;
import com.mjk.common.util.ObjectUtils;
import com.mjk.common.web.getway.constns.GetWayError;
import com.mjk.common.web.getway.constns.GetwayConsts;
import com.mjk.common.web.getway.restful.interceptor.GetWayInterceptorReq;
import com.mjk.common.web.getway.restful.interceptor.type.GetWayInterceptorType;
import com.mjk.common.web.getway.restful.prototal.BaseGetwayInterceptor;
import com.mjk.common.web.getway.transfer.GetwayTransfer;
import jakarta.websocket.Session;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

@Getter
public class WebSocketInstance {
    
	private Session session;
	
	private GetWayInterceptorReq req = new GetWayInterceptorReq();
	
	public WebSocketInstance(Session session,GetWayInterceptorReq req) {
		this.session = session;
		this.req = req;
	}

    /**
     */
    public void sendMessage(String message) {
        try {
        	//效应message参数
        	Map<String,Object> map = new HashMap<>();
			if(ObjectUtils.isNotEmpty(message)){
				if (GsonUtils.isJson(message)) {
					map.putAll(GsonUtils.gson2Map(message));						
				}else if (XmlUtils.isXML(message)) {
					//此处不支持xml 中的list
					map.putAll(XmlUtils.xmlToMap(message));
				}else {
					sendText(GsonUtils.bean2Json(GetWayError.DATA_FORMAT_ERROR.tranceError("")));
					return;
				}
			}
			if(!map.containsKey(GetwayConsts.METHOD)) {
				sendText(GsonUtils.bean2Json(GetWayError.RESTF_URL_NO_METHOD.tranceError("")));
				return;
			}
			
			this.req.setParams(map);
        	BaseGetwayInterceptor interceptor = (BaseGetwayInterceptor) BeeSpringBean.getBean("getGetWayInterceptor");
        	GetwayTransfer transfer = BeeSpringBean.getBean(GetwayTransfer.class);
        	this.req.setProtocal(transfer);
        	BeeResponse rsp = interceptor.getChains().get(GetWayInterceptorType.WEBSOCKET.getProto()).proceed(req);
            //BizLogger.info("服务端给客户端"+this.session.getId()+"发送消息"+rsp+"");
            sendText(GsonUtils.bean2Json(rsp));
        } catch (Exception e) {
        	BizLogger.error(e);
        	BeeResponse response = new BeeResponse();
    	    response.setCode(BeeError.BEE_ERROR_FOR_ADMIN.getCode());
			String error = ExceptionUtil.getMessage(e);
    	    response.setMsg(ObjectUtils.isEmpty(error)?"http service invoke exception":error);
    	    sendText(GsonUtils.bean2Json(GetWayError.RESTF_URL_NO_PROTOCAL.tranceError("")));
        }
    }
    
    public void sendText(String text) {
    	try {
    		this.session.getBasicRemote().sendText(text);
    	}catch(Exception e) {
    		//do noting
    	}
    }

	public void setSession(Session session) {
		this.session = session;
	}

	public void setReq(GetWayInterceptorReq req) {
		this.req = req;
	}
	
}
